#include"string.h"
namespace wz {
	const size_t string::npos = -1;
	string::string(const char* str) 
		:_size(strlen(str))
	{
		_capacity = _size;
		_str = new char[_capacity + 1];
		strcpy(_str, str);
	}
	string::string(const string& s) {
		_str = new char[s._capacity + 1];
		strcpy(_str, s._str);
		_size = s._size;
		_capacity = s._capacity;
	}
	void string::push_back(char ch) {
		if (_capacity == _size) {
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}
		_str[_size++] = ch;
		_str[_size] = '\0';
	}
	void string::append(const char* str) {
		size_t len = strlen(str);
		if (_capacity < _size + len) {
			size_t newcapacity = _capacity * 2;
			if (newcapacity < _size + len)
				newcapacity = _size + len;
			reserve(newcapacity);
			_capacity = newcapacity;
		}
		strcpy(_str + _size, str);
		_size += len;
	}
	void string::insert(size_t pos, char ch) {
		assert(pos <= _size && pos >= 0);
		if (_capacity == _size) {
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}
		size_t end = _size + 1;
		while (end > pos) {
			_str[end] = _str[end - 1];
			end--;
		}
		_str[pos] = ch;
		_size++;
	}
	void string::insert(size_t pos, const char* str) {
		assert(pos <= _size && pos >= 0);
		size_t len = strlen(str);
		if (_capacity < _size + len) {
			size_t newcapacity = _capacity * 2;
			if (newcapacity < _size + len)
				newcapacity = _size + len;
			reserve(newcapacity);
			_capacity = newcapacity;
		}
		size_t end = _size + len;
		while (end >= pos + len) {
			_str[end] = _str[end - len];
			end--;
		}
		for (size_t i = 0; i < len; i++)
		{
			_str[pos + i] = str[i];
		}
		_size += len;
	}
	void string::erase(size_t pos, size_t len) {
		assert(pos >= 0 && pos < _size);
		if (len >= _size - pos) {
			_str[pos] = '\0';
			_size -= len;
		}
		else {
			size_t end = pos + len;
			while (end <= _size) {
				_str[pos] = _str[end];
				pos++;
				end++;
			}
		}
	}
	size_t string::find(char ch, size_t pos) {
		assert(pos >= 0 && pos < _size);
		for (size_t i = pos; i < _size; i++){
			if (_str[i] == ch)
				return i;
		}
		return npos;
	}
	size_t string::find(const char* str, size_t pos) {
		assert(pos >= 0 && pos < _size);
		char* ret = strstr(_str + pos, str);
		if (ret != nullptr)
			return ret - _str;
		return npos;
	}

	void string::reserve(size_t n) {
		/*if (n > _capacity) {
			char* tmp = new char[_capacity+1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = new char[n + 1];
			strcpy(_str, tmp);
			_capacity = n;
		}*/
		if (n > _capacity) {
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}
	}

	string::~string() {
		delete[] _str;
		_str = nullptr;
		_size = _capacity = 0;
	}
	void string::swap(string& s) {
		std::swap(_str, s._str);
		std::swap(_size, s._size);
		std::swap(_capacity, s._capacity);
	}
	string string::substr(size_t pos, size_t len) {
		assert(pos >= 0 && pos < _size);
		if (len >= _size - pos) {
			len = _size - pos;
		}
		wz::string s;
		s.reserve(len);
		for (size_t i = pos; i < len; i++)
		{
			s += _str[pos + i];
		}
		return s;
	}
	void swap(string& s1, string& s2) {
		s1.swap(s2);
	}
	bool operator==(const string& s1, const string& s2) {
		return strcmp(s1.c_str(), s2.c_str()) == 0;
	}
	bool operator!=(const string& s1, const string& s2) {
		return !(s1.c_str() == s2.c_str());
	}
	bool operator>(const string& s1, const string& s2) {
		return strcmp(s1.c_str(), s2.c_str()) > 0;
	}
	bool operator>=(const string& s1, const string& s2) {
		return (s1.c_str() == s2.c_str()) || (s1.c_str() > s2.c_str());
	}
	bool operator<(const string& s1, const string& s2) {
		return !(s1.c_str() >= s2.c_str());
	}
	bool operator<=(const string& s1, const string& s2) {
		return (s1.c_str() < s2.c_str()) || (s1.c_str() == s2.c_str());
	}
}